Opening (and saving files)
This cant easily be done in Applets for security reasons. But its easy to do in applications. Here is an example of reading from a file.
/*
* This is the starter code for paintbrush.
* I have included 2 panels, put your tools all in the toolpanel.
* If you want to change the size, change the dimensions included
*
*
* @author Jeff Borland
* @updated 11-17-11
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class PaintBrush extends JFrame implements MouseListener,ActionListener, MouseMotionListener
{
JButton testButton=new JButton("test");
JTextField testField=new JTextField(10);
static private final String newline = "\n";
JButton openButton=new JButton("open");
JButton saveButton=new JButton("save");
JTextArea log = new JTextArea(5,20);
JFileChooser fc=new JFileChooser();;
public void init()
{
JPanel toolPanel=new JPanel();
//I create a toolpanel so you have tools lined up on the left.
// If you want to change it, change the dimension below, or see me
toolPanel.setPreferredSize(new Dimension(130,430));
toolPanel.setBorder(BorderFactory.createLineBorder(Color.black));
toolPanel.setBackground(Color.green);
//now I add my panel for drawing
JPanel drawingPanel=new JPanel();
drawingPanel.setPreferredSize(new Dimension(330,430));
drawingPanel.setBackground(Color.blue);
//add all your tools to the tool panel
toolPanel.add(testButton);
toolPanel.add(testField);
testButton.addActionListener(this);
Container screen= getContentPane();
screen.setLayout (new FlowLayout() );
//now we add the panels to the screen
screen.add(toolPanel);
screen.add(drawingPanel);
addMouseListener(this);
addMouseMotionListener(this);
openButton.addActionListener(this);
saveButton.addActionListener(this);
drawingPanel.add(log);
toolPanel.add(openButton);
toolPanel.add(saveButton);
}
public void paint (Graphics g)
{
super.paint(g);
}
//when someone presses the mouse button
public void mousePressed(MouseEvent e)
{
}
//when someone releases the mouse button
public void mouseReleased(MouseEvent e)
{
}
// when the mouse enters the applet
public void mouseEntered(MouseEvent e)
{
}
//when the mouse leaves the applet
public void mouseExited(MouseEvent e)
{
}
//when the mouse button is clicked
public void mouseClicked(MouseEvent e)
{
}
//the mouse button is pressed and the mouse makes a significantly large movement
public void mouseDragged(MouseEvent e)
{
int x=e.getX();
int y=e.getY();
Graphics g = getGraphics();
g.fillOval(x,y,25,25);
}
//the mouse makes a significantly large movement
public void mouseMoved(MouseEvent e)
{
}
public void actionPerformed(ActionEvent thisEvent)
{
Object source = thisEvent.getSource();
if (source == testButton)
{
testField.setText("test");
}
//now have if statements seeing finding out where the action occured
if (source == openButton) {
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try{
// Open the file that is the first
// command line parameter
FileInputStream fis = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
log.append(strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
//Handle save button action.
else if (source == saveButton) {
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
public static void main(String[] args) {
PaintBrush p = new PaintBrush();
p.init();
p.setSize(500,500);
p.setVisible(true);
}
}
|